package com.ruoyi.system.mysql1.mongodb;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdvancedUserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    public List<User> findUsersByCondition(String name, int minAge) {
        Query query = new Query();
        query.addCriteria(Criteria
                .where("name").is(name)
                .and("age").gte(minAge)
        );
        return mongoTemplate.find(query, User.class);
    }

    // 模糊查询
    public List<User> findByNameLike(String keyword) {
        Query query = Query.query(Criteria.where("name").regex(".*" + keyword + ".*", "i"));
        return mongoTemplate.find(query, User.class);
    }
}